package com.prs.crm.action.taskmanager;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.taskmanager.TaskType;
import com.prs.crm.service.taskmanager.TaskTypeService;

public class TaskTypeAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private PaginationSupport pagination;
	private Integer[] ids;
	private TaskType taskType;
	private TaskTypeService taskTypeService;
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public TaskType getTaskType() {
		return taskType;
	}
	public void setTaskType(TaskType taskType) {
		this.taskType = taskType;
	}
	public TaskTypeService getTaskTypeService() {
		return taskTypeService;
	}
	public void setTaskTypeService(TaskTypeService taskTypeService) {
		this.taskTypeService = taskTypeService;
	}
	
	@Action("listTaskType")
	public String listTaskType(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(TaskType.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getTaskTypeService().listByPage(criteria, pagination));
		return SUCCESS;	
	}
	
	@Action("editTaskType")
	public String editTaskType(){
		if(ids!=null){
			this.taskType=this.getTaskTypeService().get(ids[0]);
		}
		return SUCCESS;
	}
	@Action(value="saveTaskType",results={
			@Result(name="success",type="redirect",location="listTaskType"),
			@Result(name="error",type="chain",location="editTaskType")
	})
	public String saveTaskType(){
		if (taskType.getName()==null ||"".equals(taskType.getName().trim())) {
			this.addActionError("类别名称不能为空！");
			return ERROR;
		}
		this.getTaskTypeService().save(taskType);
		return SUCCESS;
	}
	@Action(value="removeTaskType",results={
			@Result(name="success",type="redirect",location="listTaskType"),
			@Result(name="error",type="chain",location="listTaskType")
	})
	public String removeTaskType(){
		if(ids!=null){
			this.taskType=this.getTaskTypeService().get(ids[0]);
		}
		this.getTaskTypeService().batchRemove(ids);
		return SUCCESS;
	}
}
